class Role < ActiveRecord::Base

  has_and_belongs_to_many :users
  belongs_to :coordinated, :polymorphic => true
  
  has_many :report_responses, :foreign_key => 'owner_role_id'
  
  
  # Returns the types of the coordinated objects by a role (site, country, strategy)
  def self.types
    [Site, Country, Strategy]
  end
  
  # Returns the description for a given type
  def self.description(type)
    DESCRIPTIONS[type]
  end
  
  # Returns the description in plural for a given type
  def self.description_plural(type)
    DESCRIPTIONS_PLURAL[type]
  end
  
  # Returns the kind code for a given type
  def self.kind(type)
    KINDS[type]
  end
  
  # Returns the description of the role
  def description
    if self.coordinated.code.nil?
      DESCRIPTIONS[self.coordinated_type.constantize]
    else
      DESCRIPTIONS_FORMAT[self.coordinated_type.constantize] % self.coordinated.code
    end
  end
  
  def description_for_reports
    if self.is_strategy_coordinator
      self.coordinated.grouping.tab_name + _(" Coordinator Reports")
    else
      DESCRIPTIONS[self.coordinated_type.constantize]+ " " + self.coordinated.name + _(" Reports")
    end
  end
  
  def description_for_report_title
    if self.is_strategy_coordinator
      self.coordinated.grouping.tab_name + _(" Coordinator Report")
    else
      DESCRIPTIONS[self.coordinated_type.constantize]+  _(" Report for ") + self.coordinated.name
    end
  end
  
  def self.find_all_by_type(type)
    Role.find(:all, :conditions => ["coordinated_type=?", type])
  end
  
  
  # Returns the description of the role pluralized
  def description_pluralized
    return DESCRIPTIONS_PLURAL[self.coordinated_type.constantize]
  end
  
  # Returns xb, sc or cc based on role kind
  def kind
    return KINDS[self.coordinated_type.constantize]
  end

  # Returns the role of the XB managers that belong to the country coordinated by this CC (or nil if current is not cc)
  def coordinated_xbs
    Site.find_all_by_country_id(self.country.id).map(&:coordinator_role).reject(&:nil?) if self.is_country_coordinator
  end
  
  def is_xb
    is_cross_border
  end
  
  def is_cross_border
    self.coordinated_type.constantize == Site
  end
  
  def is_country_coordinator
    self.coordinated_type.constantize == Country
  end
  
  def is_strategy_coordinator
    self.coordinated_type.constantize == Strategy
  end

  def country
    return self.coordinated if self.is_country_coordinator
  end
  
  def strategy
    return self.coordinated if self.is_strategy_coordinator
  end
  
  def site
    return self.coordinated if self.is_cross_border
  end
  
  def code
    return self.site.code if self.is_cross_border
  end

  private

  DESCRIPTIONS = { 
    Site => _('Cross Border Manager'),
    Country => _('Country Strategy Coordinator'),
    Strategy => _('Lead Strategy Coordinator')    
  } 
  
  DESCRIPTIONS_FORMAT = { 
    Site => 'Cross Border %s Manager',
    Country => 'Country %s Coordinator',
    Strategy => 'Strategy %s Coordinator'
  }
  
  DESCRIPTIONS_PLURAL = { 
    Site => _('Cross Border Managers'),
    Country => _('Country Strategy Coordinators'),
    Strategy => _('Lead Strategy Coordinators')
  } 

  KINDS = { 
    Site => 'xb',
    Country => 'cc',
    Strategy => 'sc'
  } 
  
end
